package com.moleq.dialog;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.SmartBeep;
import com.moleq.device.DeviceManager;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.enums.ItemType;
import com.moleq.logic.TransactionManager;
import com.moleq.models.CouponSetting;
import com.moleq.models.FreeFuncRpt;
import com.moleq.models.GlobalInfo;
import com.moleq.mgpos.activities.R;
import com.moleq.service.ServiceManager;
import com.moleq.util.MoleQUtil;

public class CouponDialog extends MoleQBaseDialog {

	private Button mButtonBack,mButtonOk,mButtonRemove;
	private TransactionManager mPaymentMgr;
	private Context mContext;
	private ListView mListView;
	private List<CouponSetting> mDataSource = new  ArrayList<CouponSetting>();
	private OpenCouponAdapter mAdapter;
	private DeviceManager mDeviceManager; //contral device scanner or scale
	private int selectedRow = 0;
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View v) {	
			switch(v.getId()){
			case R.id.buttonCouponSelectionClose:
				mDeviceManager.enableAllScanner();
				dismiss();
				break;
			case R.id.buttonCouponSelectionRemove:
				
				MessageBox.show(mContext, "Do you want to remove the item coupon?",mOnClickMessageListener,MessageDialog.MESSAGE_BOX_YES_NO);

				break;
			case R.id.buttonCouponSelectionOk:	
				
				if (mDataSource.size() > 0) {
					CouponSetting mCouponSetting = mDataSource.get(selectedRow);
					if (mPaymentMgr.getSubTotal() < mCouponSetting.getCs_min_purchase()) {
						MessageBox.show(mContext, mContext.getResources().getString(R.string.msg_coupon_total_less_purchase));
					}else{
						GlobalInfo.getInstance().useFuntionsList.add(GlobalInfo.getInstance().CurrentFreeFuncID);
						TransactionManager.getInstance().SaveFreeFuncRpt(GlobalInfo.getInstance().CurrentFreeFuncID, 1, mCouponSetting.getCs_value() * (-1));
						mPaymentMgr.setCouponSetting(mCouponSetting);
						mPaymentMgr.mSaleListItems.announceCartChanges();
						mDeviceManager.enableAllScanner();
						dismiss();
					}
				}
				break;
			}
		}
	};
	
	public ListView.OnItemClickListener onItemClickListener = new ListView.OnItemClickListener() {


		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			switch (parent.getId()) {
			case R.id.lv_dialog_coupon_list:
				ScrollToRow(position);
				break;
			}
		}
	};
	
	
	public CouponDialog(Context context) {
		super(context,R.style.dialog,context);
		constructor(context);
		
	}

	public CouponDialog(Context context, int theme) {
		super(context, theme);
		constructor(context);
	}
	
	private void constructor(Context context) {
		// TODO Auto-generated method stub
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// set UI layout
		this.setContentView(R.layout.dialog_coupon_selection);
		this.setCancelable(false);
	
		mButtonBack = (Button) findViewById(R.id.buttonCouponSelectionClose);
		mButtonBack.setOnClickListener(mOnClickListener);
		
		mButtonOk = (Button) findViewById(R.id.buttonCouponSelectionOk);
		mButtonOk.setOnClickListener(mOnClickListener);
		
		mButtonRemove = (Button) findViewById(R.id.buttonCouponSelectionRemove);
		mButtonRemove.setOnClickListener(mOnClickListener);
		
		mListView = (ListView) findViewById(R.id.lv_dialog_coupon_list);
		mListView.setOnItemClickListener(onItemClickListener);
		
		mAdapter = new OpenCouponAdapter(mContext);
		mListView.setAdapter(mAdapter);
		mListView.setDivider(null);
	}
	
	public void show(){
		super.show();		
		
		mDeviceManager = DeviceManager.getInstance();
		mPaymentMgr =  TransactionManager.getInstance();
		mDeviceManager.disableAllScanner();
		
		mDataSource =  ServiceManager.getInstance().mCouponSettingService.FindAll();
		mAdapter.notifyDataSetChanged();
	}
	
	class OpenCouponAdapter extends BaseAdapter{
		private Context maContext;
		public final class ViewHolder{
			public TextView tv_coupon_name;
			public TextView tv_coupon_value;
			public TextView tv_coupon_min_purchase;
		}
		
		public OpenCouponAdapter(Context context){
			maContext = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mDataSource.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return mDataSource.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ViewHolder holder = null;
			if(convertView == null){
				holder = new ViewHolder();
				
				convertView = LayoutInflater.from(maContext).inflate(R.layout.coupon_list_item, null);
				holder.tv_coupon_name = (TextView) convertView.findViewById(R.id.tv_coupon_name);
				holder.tv_coupon_value = (TextView) convertView.findViewById(R.id.tv_coupon_value);
				holder.tv_coupon_min_purchase = (TextView) convertView.findViewById(R.id.tv_coupon_min_purchase);
				
				convertView.setOnTouchListener(onItemTouchListener);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			
			String content = mDataSource.get(position).getCs_desc();
			double value = mDataSource.get(position).getCs_value() * -1;
			double min_purchase = mDataSource.get(position).getCs_min_purchase();
			
			holder.tv_coupon_name.setText(content);
			holder.tv_coupon_value.setText(MoleQUtil.fixNumber(value) );
			holder.tv_coupon_min_purchase.setText(MoleQUtil.fixNumber(min_purchase));
			
			if (position == selectedRow) {
				holder.tv_coupon_name.setBackgroundColor(Color.rgb(255,219,16));
				holder.tv_coupon_value.setBackgroundColor(Color.rgb(255,219,16));
				holder.tv_coupon_min_purchase.setBackgroundColor(Color.rgb(255,219,16));
			} else {
				holder.tv_coupon_name.setBackgroundColor(Color.rgb(204, 204, 204));
				holder.tv_coupon_value.setBackgroundColor(Color.rgb(204, 204, 204));
				holder.tv_coupon_min_purchase.setBackgroundColor(Color.rgb(204, 204, 204));
			}
//			
			return convertView;
		}
		
	}
	
	private OnTouchListener onItemTouchListener = new OnTouchListener(){

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			if(event.getAction() == MotionEvent.ACTION_DOWN){
				SmartBeep.getInstance().play(SmartBeep.SOUND_CLICK);
			}
			return false;
		}
		
	};
	
	public void ScrollToRow(int row){
		selectedRow = row;
		mAdapter.notifyDataSetChanged();
	}

private OnClickMessageListener mOnClickMessageListener = new OnClickMessageListener(){
		
		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub
			
			if(mPaymentMgr.getCouponSetting() != null){
				GlobalInfo.getInstance().useFuntionsList.add(GlobalInfo.getInstance().CurrentFreeFuncID);
				TransactionManager.getInstance().SaveFreeFuncRpt(GlobalInfo.getInstance().CurrentFreeFuncID, 1, mPaymentMgr.getCouponSetting().getCs_value());
				mPaymentMgr.setCouponSetting(null);
				mPaymentMgr.mSaleListItems.announceCartChanges();
			}
			mDeviceManager.enableAllScanner();
			dismiss();
		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onClickOK() {
			
		}
		
};
}
